using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.AI;
using UnityEngine.Serialization;

public class ScenceController : singleTon<ScenceController>,IEndGameObserver
{
    GameObject player;
    public GameObject SaberPrefab;
    public GameObject ArcherPrefab;
    public GameObject MagePrefab;

    public ScenceFader ScenceFaderPrefeb;

    bool isDie = true;

    protected override void Awake()
    {
        base.Awake();
        DontDestroyOnLoad(this);
    }
    private void Start()
    {
        GameManager.Instance.AddObverser(this);
    }
    public void TransitionToDestination(TransitionPoint transitionPoint)
    {
        switch (transitionPoint.transitionType)
        {
            case TransitionPoint.TransitionType.SameScence:
                StartCoroutine(Transition(SceneManager.GetActiveScene().name, transitionPoint.destinationTag));
                break;

            case TransitionPoint.TransitionType.DifferentScence:
                StartCoroutine(Transition(transitionPoint.scenceName, transitionPoint.destinationTag));
                break;
        }
    }

    public void TransitionToFirstLevel()
    {
        StartCoroutine(LoadLevel("Village"));
    }
    public void TransitionToLoadGame()
    {
        StartCoroutine(LoadLevel(SaveManager.Instance.ScenceName));
    }
    public void TransitionToMain()
    {
        StartCoroutine(LoadMain());
    }
    IEnumerator Transition(string scenceName,TransitionDestination.DestinationTag destinationTag)
    {
        //TODO:保存一些数据
        for(int i=0;i<4;i++)
        {
            SkillManager.Instance.HaveCD[i] = false;
        }
        SaveManager.Instance.SavePlayerData();
        if (SceneManager.GetActiveScene().name != scenceName)
        {
            ScenceFader scenceFader = Instantiate(ScenceFaderPrefeb);
            yield return StartCoroutine(scenceFader.FadeOut(1.0f));
            yield return SceneManager.LoadSceneAsync(scenceName);
            yield return Instantiate(SaberPrefab, GetDestination(destinationTag).transform.position, GetDestination(destinationTag).transform.rotation);
            yield return Instantiate(ArcherPrefab, GetDestination(destinationTag).transform.position, GetDestination(destinationTag).transform.rotation);
            yield return Instantiate(MagePrefab, GetDestination(destinationTag).transform.position, GetDestination(destinationTag).transform.rotation);
            yield return StartCoroutine(scenceFader.FadeIn(1.0f));
            SaveManager.Instance.LoadPlayerData();
            yield break;
        }
        else
        {
            player = GameManager.Instance.playerStats.gameObject;
            player.GetComponent<NavMeshAgent>().enabled = false;
            player.transform.SetPositionAndRotation(GetDestination(destinationTag).transform.position, GetDestination(destinationTag).transform.rotation);
            player.GetComponent<NavMeshAgent>().enabled = true;
            yield return null;
        }
    }
    IEnumerator LoadLevel(string scence)
    {
        ScenceFader scenceFader = Instantiate(ScenceFaderPrefeb);
        if (scence != "")
        {
            yield return StartCoroutine(scenceFader.FadeOut(2.0f));
            yield return SceneManager.LoadSceneAsync(scence);
            yield return Instantiate(SaberPrefab,GameManager.Instance.GetEntrance().position, GameManager.Instance.GetEntrance().rotation);
            yield return Instantiate(ArcherPrefab,GameManager.Instance.GetEntrance().position, GameManager.Instance.GetEntrance().rotation);
            yield return Instantiate(MagePrefab,GameManager.Instance.GetEntrance().position, GameManager.Instance.GetEntrance().rotation);
            
            SaveManager.Instance.SavePlayerData();
            yield return StartCoroutine(scenceFader.FadeIn(2.0f));
            yield break;
        }
    }

    IEnumerator LoadMain()
    {
        ScenceFader scenceFader = Instantiate(ScenceFaderPrefeb);
        yield return StartCoroutine(scenceFader.FadeOut(2.0f));
        yield return SceneManager.LoadSceneAsync("Main");
        yield return StartCoroutine(scenceFader.FadeIn(2.0f));
        yield break;
    }
    private TransitionDestination GetDestination(TransitionDestination.DestinationTag destinationTag)
    {
        var entrances = FindObjectsOfType<TransitionDestination>();
        foreach(var entrance in entrances)
        {
            if (entrance.destinationTag == destinationTag)
            {
                return entrance;
            }
        }
        return null;
    }

    public void EndNotify()
    {
        if (isDie)
        {
            isDie = false;
            StartCoroutine(LoadLevel("Village"));
        }
    }
}
